package com.zztt.logmask.log4j;

import com.zztt.logmask.common.utils.MaskHandleUtils;
import org.apache.log4j.RollingFileAppender;
import org.apache.log4j.spi.LoggingEvent;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 替换PatternLayout为扩展后的 CupdataZiconPatternLayout  并扩展一些自有属性
 */
public class CupdataZiconRollingFileAppenderFilter extends RollingFileAppender {
    private AtomicBoolean atomicBoolean = new AtomicBoolean(false);

    @Override
    protected void subAppend(LoggingEvent event) {
        replaceLayout();
        super.subAppend(event);
    }

    /**
     * 替换 PatternLayout为扩展后的 CupdataZiconPatternLayout
     */
    private void replaceLayout(){
        if(atomicBoolean.compareAndSet(false,true)){
            this.setLayout(new CupdataZiconPatternLayout(this.getLayout()));
        }
    }

    /**
     * 设置多个掩码处理器  顺序根据配置文件中的顺序进行
     * @param maskHandles
     */
    public void setMaskHandles(String maskHandles) throws Exception{
        MaskHandleUtils.setMaskHandles(maskHandles);
    }

    /**
     * 设置 掩码处理器是否开启
     * @param maskHandlesEnabled
     */
    public void setMaskHandlesEnabled(boolean maskHandlesEnabled){
        MaskHandleUtils.setMaskHandlesEnabled(maskHandlesEnabled);
    }

    /**
     * 设置 掩码处理器支持的日志级别
     * @param maskHandleSuportLevels
     */
    public void setMaskHandleSuportLevels(String maskHandleSuportLevels){
        MaskHandleUtils.setMaskHandleSuportLevels(maskHandleSuportLevels);
    }


}
